package com.czn.shopdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.czn.shopdemo.model.Business;
import com.czn.shopdemo.model.Result;
import com.czn.shopdemo.service.BusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Zenan Chen
 * @date 2023/4/3 17:54
 */
@RestController
@RequestMapping("/business")
@Api(tags = "商户管理")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    private static final Logger log = LoggerFactory.getLogger(BusinessController.class);

    @ApiOperation("查询商户列表")
    @PostMapping("/getBusinessList")
    public Result getBusinessList() {
        try {
            log.info("------------------------->/getBusinessList 查询商户列表开始");
            List<Business> result = businessService.getBusinessList();
            log.info("------------------------->/getBusinessList 查询商户列表结束");
            return Result.ok(result);
        } catch (Exception e) {
            log.error("------------------------->/getBusinessList 查询商户列表异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("获取商户详情")
    @PostMapping("/getBusinessDetail")
    public Result getBusinessDetail(@RequestParam String uuid) {
        try {
            log.info("接受到参数：uuid:{}", uuid);
            log.info("------------------------->/getBusinessDetail 获取商户详情开始");
            Business Business = businessService.getBusinessDetail(uuid);
            log.info("------------------------->/getBusinessDetail 获取商户详情结束");
            return Result.ok(Business);
        } catch (Exception e) {
            log.error("------------------------->/getBusinessDetail -获取商户详情异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("保存商户")
    @PostMapping("/saveBusiness")
    public Result save(@RequestBody Business business, HttpServletRequest request) {
        try {
            log.info("接受到参数：business:{}", JSONObject.toJSONString(business));
            log.info("------------------------->/saveBusiness 保存商户开始");
            businessService.saveBusiness(business,request);
            log.info("------------------------->/saveBusiness 保存商户结束");
            return Result.ok("保存商户成功！");
        } catch (Exception e) {
            log.error("------------------------->/saveBusiness 保存商户异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("删除商户")
    @PostMapping("/deleteBusiness")
    public Result deleteBusiness(@RequestParam String uuid) {
        try {
            log.info("接受到参数：uuid:{}", uuid);
            log.info("------------------------->/saveBusiness 删除商户开始");
            businessService.deleteBusiness(uuid);
            log.info("------------------------->/saveBusiness 删除商户结束");
            return Result.ok("删除商户成功！");
        } catch (Exception e) {
            log.error("------------------------->/saveBusiness 删除商户异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }
}
